package com.ruoyi.web.controller.shop.service.Impl;

import com.ruoyi.common.entity.DO.shop.ShopConfigDO;
import com.ruoyi.common.entity.VO.shop.ShopConfigSearchVO;
import com.ruoyi.common.entity.VO.shop.ShopConfigVO;
import com.ruoyi.common.exception.BusinessException;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.system.mapper.IBackShopConfigMapper;
import com.ruoyi.web.controller.shop.mapper.IBackShopMapper;
import com.ruoyi.web.controller.shop.service.IBackShopConfigService;
import org.springframework.stereotype.Service;
import tk.mybatis.mapper.entity.Example;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

@Service
public class BackShopConfigServiceImpl implements IBackShopConfigService {

    @Resource
    private IBackShopConfigMapper configMapper;

    @Resource
    private IBackShopMapper shopMapper;

    @Override
    public List<ShopConfigVO> searchList(ShopConfigSearchVO shopConfigSearchVO) {
        return configMapper.searchList(shopConfigSearchVO);
    }

    @Override
    public int update(ShopConfigVO shopConfigVO) {
        if (shopConfigVO.getShopId() == 0) {
            throw new BusinessException("请先切换站点！");
        }
        Example example = new Example(ShopConfigDO.class);
        example.createCriteria().andEqualTo("configId", shopConfigVO.getConfigId())
                .andEqualTo("shopId", shopConfigVO.getShopId());
        ShopConfigDO shopConfigDO = configMapper.selectOneByExample(example);
        if (shopConfigDO == null) {
            //新增
            shopConfigDO = new ShopConfigDO();
            shopConfigDO.setConfigId(shopConfigVO.getConfigId());
            shopConfigDO.setShopId(shopConfigVO.getShopId());
            shopConfigDO.setConfigValue(shopConfigVO.getConfigValue());
            shopConfigDO.setCreateTime(new Date());
            return configMapper.insertSelective(shopConfigDO);
        } else {
            //修改
            shopConfigDO.setConfigValue(shopConfigVO.getConfigValue());
            shopConfigDO.setUpdateBy(shopConfigVO.getUpdateBy());
            shopConfigDO.setUpdateTime(new Date());
            return configMapper.updateByPrimaryKeySelective(shopConfigDO);
        }
    }

    @Override
    public void refresh(Long shopId,boolean isInit) {
        //TODO:查询店铺是否已经审核通过
        int count = shopMapper.checkShopIsReview(shopId);
        if (count == 1 || isInit) {
            count = configMapper.countConfig();
            List<Long> configIdList = configMapper.countShopConfig(shopId);
            if (count > configIdList.size()) {
                List<ShopConfigDO> configList;
                //配置初始化
                String ids = StringUtils.join(configIdList, ",");
                if(StringUtils.isNotEmpty(ids)){
                    configList = configMapper.searchBackList(ids);
                }else{
                    configList = configMapper.searchBackAllList();
                }
                for (ShopConfigDO shopConfigDO : configList
                ) {
                    shopConfigDO.setShopId(shopId);
                    shopConfigDO.setCreateBy("系统初始化");
                    shopConfigDO.setCreateTime(new Date());
                    configMapper.insertSelective(shopConfigDO);
                }
            }
        }
    }
}
